package pl.com.sthaether.mc.components.treetable.filterable;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.TreeItem;
import pl.com.sthaether.mc.components.table.filterable.MCFilterableItem;
import pl.com.sthaether.mc.engine.data.MCMapDataObject;

import java.util.Map;

public class MCFilterableTreeTableNode implements MCFilterableItem {

    //TODO  - to complete
    private StringProperty name;
    private DoubleProperty value;

    public MCFilterableTreeTableNode(StringProperty name, DoubleProperty value) {
        this.name = name;
        this.value = value;
    }

    public static TreeItem generateMapObjectTree(MCMapDataObject object) {
        TreeItem root = new TreeItem(new MCFilterableTreeTableNode(object.getText().textProperty(), null));//object.objectValueProperty()
        TreeItem chart = new TreeItem(new MCFilterableTreeTableNode(new SimpleStringProperty("Chart series"), null));


        root.getChildren().add(new TreeItem(new MCFilterableTreeTableNode(new SimpleStringProperty("Value"), object.objectValueProperty())));
//        root.getChildren().add(new TreeItem(new MCFilterableTreeTableNode (new SimpleStringProperty("Chart value"), object.chartValueProperty())));

        for (Map.Entry<String, DoubleProperty> entry : object.getChartData().entrySet()) {
            chart.getChildren().add(new TreeItem(new MCFilterableTreeTableNode(new SimpleStringProperty(entry.getKey()), entry.getValue())));
        }
//        chart.getChildren().add(new TreeItem(new MCFilterableTreeTableNode(new SimpleStringProperty("Add"), new SimpleDoubleProperty(0D))));
        root.getChildren().add(chart);
        return root;
    }

    public StringProperty nameProperty() {
        return name;
    }

    public DoubleProperty nodeValueProperty() {
        return value;
    }

    @Override
    public boolean matchesText(String text) {
        return name.getValue().toLowerCase().contains(text.trim().toLowerCase());
    }
}
